package controllers

import (
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"user/models"
	"user/utils"
)

type LiveController struct {
	beego.Controller
}

// @router /live/create [*]
func (this *LiveController) CreateLiveRoom() {

	title := this.GetString("title")
	uid, _ := this.GetInt("uid")
	image := this.GetString("image")

	if uid == 0 {
		this.Data["json"] = ApiFailed(4001, "请先登录")
		this.ServeJSON()
		this.StopRun()
	}

	if title == "" {
		this.Data["json"] = ApiFailed(4002, "标题不能为空")
		this.ServeJSON()
		this.StopRun()
	}

	if title == "" {
		this.Data["json"] = ApiFailed(4003, "标题不能为空")
		this.ServeJSON()
		this.StopRun()
	}

	//添加默认直播间封面图
	if image == "" {
		image = "https://cdn.pixabay.com/photo/2023/03/22/20/16/muffin-7870491_640.jpg"
	}

	//获取直播推拉流地址
	res := utils.GetLiveUrl(uid, 0)
	fmt.Println(res)
	//创建直播 push_url string, live_url string
	err := models.CreateLiveRoom(uid, title, image, res["push_url"], res["push_key"], res["flv_live_url"])
	fmt.Println(err)
	if err != nil {
		this.Data["json"] = ApiFailed(4004, "添加直播失败")
		this.ServeJSON()
		this.StopRun()
	}

	this.Data["json"] = ApiSucc(0, "success", res, 1)
	this.ServeJSON()
	this.StopRun()
}

func ApiSucc(i int, s string, res map[string]string, i2 int) interface{} {
	return nil
}

func ApiFailed(i int, s string) interface{} {
	return nil
}
